package sdk.webview.fmc.com.fmcsdk.view;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import sdk.webview.fmc.com.fmcsdk.R;


/**
 * 自定的dialog提示框，包括GPS开关，清理缓存，更新APK
 */
public class GPSCustomDialog extends Dialog {
	private Context context;
	/**标题和内容*/
	private String msg,title;
	/**是否是清理缓存打开的提示框*/
	private boolean isClearData = false;
	private ClickListenerInterface clickListenerInterface;
	private TextView confirm;

	//只有提示内容的消息框
	public GPSCustomDialog(Context context,String msg) {
		super(context, R.style.Dialog);
		this.context = context;
		this.msg = msg;
	}
	//带有自定义标题的提示框
	public GPSCustomDialog(Context context,String title,String msg) {
		super(context, R.style.Dialog);
		this.context = context;
		this.msg = msg;
		this.title = title;
		isClearData = true;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gps_custom_dialog);
		TextView tv_title = (TextView) findViewById(R.id.tv_title);
		confirm = (TextView) findViewById(R.id.confirm);
		TextView cancel = (TextView) findViewById(R.id.cancel);
		TextView tv_message = (TextView) findViewById(R.id.tv_message);
		//如果是清理缓存跳转过来的，将有些文字标红
		if (isClearData){
			//设置标题和内容
			tv_title.setText(title);
			SpannableString spannableString = new SpannableString(msg);
			SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
			int language = sharedPreferences.getInt("lang", 0);
			if (language!=1) {
				spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#e34a4c")), 5, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
			tv_message.setText(spannableString);
		}else {
			tv_message.setText(msg);
		}
		confirm.setOnClickListener(new clickListener());
		cancel.setOnClickListener(new clickListener());

		//试着提示框的位置和大小
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
		lp.width = (int) (d.widthPixels * 0.9);
		dialogWindow.setAttributes(lp);

	}

	public void setConfirmText(String text){
		confirm.setText(text);
	}

	public void setClicklistener(ClickListenerInterface clickListenerInterface) {
		this.clickListenerInterface = clickListenerInterface;
	}

	private class clickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			int id = v.getId();
			if (id==R.id.confirm){
				clickListenerInterface.doConfirm();
			}else if (id==R.id.cancel){
				clickListenerInterface.doCancel();
			}

		}

	}


	public interface ClickListenerInterface {

		void doConfirm();

		void doCancel();
	}

}
